<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'security_alarm:alarmInfo:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.deviceName }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'securityAlarm:alarmInfo:edit'" type="text">
          <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <Popconfirm
          title="是否确认删除安全告警表"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button v-auth="'securityAlarm:alarmInfo:edit'" type="text" style="color: red;">
            <span style="font-size: 14px;">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsSecurityAlarmAlarmInfoList">
import { unref, ref, computed } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { Icon } from '/@/components/Icon';
import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
import { alarmInfoDelete, alarmInfoListData } from '/@/api/safealarm/safealarm';
import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
import { useDrawer } from '/@/components/Drawer';
import { FormProps } from '/@/components/Form';
import InputForm from './form.vue';
import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件
import { useRoute } from 'vue-router'; // 引入路由钩子

const { t } = useI18n('security_alarm.alarmInfo');
const { showMessage } = useMessage();
const { meta } = unref(router.currentRoute);
const route = useRoute();
console.log('router',route.query)

const EnterpriseName = ref('');
const EnterpriseParams = computed(() => {
  return {
    name: EnterpriseName.value,
  };
});
const getTitle = {
  icon: meta.icon || 'i-ant-design:book-outlined',
  value: meta.title || t('安全告警表管理'),
};

const searchForm: FormProps = {
  baseColProps: { md: 8, lg: 6 },
  labelWidth: 90,
  schemas: [
    {
      label: t(''),
      field:"alarmTypeCode",
      component: 'Select',
      componentProps: {
        placeholder: t('请输入告警类型'),
        dictType: 'alarm_type',
        fieldNames: { label: 'label', value: 'value' },
        allowClear: true,
        defaultValue: 'Illegal_occupation_fire_lanes' // 添加默认值
      },
    },
    {
      label: t(''),
      field: 'alarmName',
      component: 'Input',
      componentProps: {
        placeholder: t('请输入告警名称'),
      },
    },
    {
      label: t(''),
      field: 'enterpriseCode',
      fieldLabel:"enterpriseName",
      component: 'TreeSelect',
      componentProps: {
        placeholder: t('请输入主体名称'),
        api: unitEnterpriseDominantListData,
        fieldNames: { label: 'name', value: 'unitCode' },
        resultField: 'list',
        params: EnterpriseParams,
        allowClear: true,
        immediate: false,
        each: false,
        showSelect: true,
        optionFilterProp: 'label',
        showSearch: true,
        onSearch: (value) => {
          EnterpriseName.value = value;
          // console.log('输入的值:', value);
          // reload()
        },
      },
    },
  ],
};

const tableColumns: BasicColumn[] = [
  {
    title: t('告警名称'),
    dataIndex: 'alarmName',
    key: 'a.alarm_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('企业名称'),
    dataIndex: 'enterpriseName',
    key: 'a.enterprise_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('上报时间'),
    dataIndex:'reportTime',
    key: 'a.report_time',
    sorter: true,
    width: 130,
    align: 'center',
  },
  {
    title: t('处理状态'),
    dataIndex: 'handleStatus',
    key: 'a.handle_status',
    sorter: true,
    width: 130,
    align: 'center',
    dictType:'safe_alarm_status',
  },
  {
    title: t('处理人'),
    dataIndex: 'handlerName',
    key: 'a.handler_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('操作'),
    dataIndex: 'action',
    key: 'action',
    width: 160,
    fixed: 'right',
    slot: 'action' // 指定使用 action 插槽
  }
];

const [registerTable, { reload }] = useTable({
  api: alarmInfoListData,
  beforeFetch: (params) => {
   if(route.query){
    params.alarmTypeCode = route.query.alarmTypeCode;
    params.tid = route.query.tid;
   }
    return params;
  },
  
  columns: tableColumns,
  // actionColumn: actionColumn, // 移除原有的 actionColumn 配置
  formConfig: searchForm,
  showTableSetting: true,
  useSearchForm: true,
  canResize: true,
});

const [registerDrawer, { openDrawer }] = useDrawer();

function handleForm(record: Recordable) {
  openDrawer(true, record);
}

async function handleDelete(record: Recordable) {
  const params = { tid: record.tid };
  const res = await alarmInfoDelete(params);
  showMessage(res.message);
  handleSuccess(record);
}

function handleSuccess(record: Recordable) {
  reload({ record });
}

function handleCancel() {
  // 取消删除操作时的处理逻辑，这里可以不做任何处理，仅为了响应取消事件
  console.log('取消删除操作');
}
</script>